package com.liuyi.framework.factory;

import com.liuyi.framework.init.SpringInitConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr.fmy
 * @version 1.0.0
 * @ClassName SpringTypeFactoryHolder
 * @Description TODO SpringTypeFactory掌控类
 * @createTime 2020/4/25 17:13
 */
@Component
public class SpringTypeFactoryHolder implements SpringInitConfig.SpringInit {

    private static final Map<Class, Map<String, ? super SpringTypeFactory>> vs = new HashMap<>();

    @Override
    public void init(ApplicationContext context) {
        Map<String, SpringTypeFactory> beansOfType = context.getBeansOfType(SpringTypeFactory.class);
        beansOfType.forEach((k, v) -> {
            Class<?> superclass = v.getClass().getSuperclass();
            Map<String, ? super SpringTypeFactory> map = vs.computeIfAbsent(superclass, k1 -> new HashMap<>());
            map.put(k, v);
        });
    }

    public static Map<Class, Map<String, ? super SpringTypeFactory>> getVs() {
        return vs;
    }
}
